﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CAD.Service
{
    public static class SelectionExtension
    {

        [CommandMethod("SelectEntity")]
        public static IList<Entity> SelectEntity()
        {
            List<Entity> entitys = new List<Entity>();

            //-----------

            //当前选项卡文档
            Document document = Application.DocumentManager.MdiActiveDocument;
            //编辑器
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;


            PromptSelectionResult promptSelRes = editor.SelectImplied();//得到选择集状态
            SelectionSet selectionSet;

            //判断是否有已选项  其实反正都要清空选择  此步判断可跳过
            //if (promptSelRes.Status == PromptStatus.OK)
            //{
            //    selectionSet = promptSelRes.Value;
            //    editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
            //}
            //else
            //{
            //    editor.WriteMessage($"selected num :0");
            //}

            //创建空数组
            ObjectId[] emptyObjectId = new ObjectId[0];
            editor.SetImpliedSelection(emptyObjectId);  //设置选择集 指向空数组

            //开始请求从图形工程界面选择图元
            promptSelRes = editor.GetSelection();

            //判断是否选中
            if (promptSelRes.Status == PromptStatus.OK)
            {
                selectionSet = promptSelRes.Value;
                editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
                List<ObjectId> objectIds = selectionSet.GetObjectIds().ToList();

                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction transaction = database.TransactionManager.StartTransaction())
                {
                    objectIds.ForEach(oid => entitys.Add((Entity)oid.GetObject(OpenMode.ForRead)));
                    transaction.Commit();
                }
            }
            else
            {
                editor.WriteMessage($"selected num :0");
            }

            return entitys;
        }


        public static IList<Entity> SelectEntity(this Editor editor)
        {
            List<Entity> entitys = new List<Entity>();

            //----------- 
            PromptSelectionResult promptSelRes = editor.SelectImplied();//得到选择集状态
            SelectionSet selectionSet;

            //创建空数组
            ObjectId[] emptyObjectId = new ObjectId[0];
            editor.SetImpliedSelection(emptyObjectId);  //设置选择集 指向空数组

            //开始请求从图形工程界面选择图元
            promptSelRes = editor.GetSelection();

            //判断是否选中
            if (promptSelRes.Status == PromptStatus.OK)
            {
                selectionSet = promptSelRes.Value;
                editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
                List<ObjectId> objectIds = selectionSet.GetObjectIds().ToList();

                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction transaction = database.TransactionManager.StartTransaction())
                {
                    objectIds.ForEach(oid => entitys.Add((Entity)oid.GetObject(OpenMode.ForRead)));
                    transaction.Commit();
                }
            }
            else
            {
                editor.WriteMessage($"selected num :0");
            }

            return entitys;
        }


        public static IList<T> SelectEntity<T>(this Database database) where T : class
        {
            var editor = database.GetEditor();
            List<T> entitys = new List<T>();

            //----------- 
            PromptSelectionResult promptSelRes = editor.SelectImplied();//得到选择集状态
            SelectionSet selectionSet;

            //创建空数组
            ObjectId[] emptyObjectId = new ObjectId[0];
            editor.SetImpliedSelection(emptyObjectId);  //设置选择集 指向空数组

            //开始请求从图形工程界面选择图元
            promptSelRes = editor.GetSelection();

            //判断是否选中
            if (promptSelRes.Status == PromptStatus.OK)
            {
                selectionSet = promptSelRes.Value;
                editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
                List<ObjectId> objectIds = selectionSet.GetObjectIds().ToList();


                using (Transaction transaction = database.TransactionManager.StartTransaction())
                {
                    entitys = objectIds
                        .Select(oid => oid.GetObject(OpenMode.ForRead))
                        .Where(obj => obj is T)
                        .Select(obj => obj as T)
                        .Where(t => t != null)
                        .ToList();
                    //objectIds.ForEach(oid => entitys.Add((T)oid.GetObject(OpenMode.ForRead)));
                    transaction.Commit();
                }
            }
            else
            {
                editor.WriteMessage($"selected num :0");
            }

            return entitys;
        }





    }
}
